<template>
	<view class="mp_info">
		<view class="mp_sum">
			<text class="sum_text">总发布</text>
			<text class="sum">
				{{sum}}
			</text>
			<button class="mp_button" @click="send">我要发布</button>
		</view>
		<view class="tabs_info">
			<SubSection :list="list" @change="sectionChange" class="sticky"></SubSection>
			<view v-for="item in dataList" :key="item['id']">
				<PublishCard :product="item" @delete="deleteProduct"></PublishCard>
			</view>
		</view>
		<u-back-top :scroll-top="scrollTop" mode="square" tips="顶部"></u-back-top>
	</view>
</template>

<script>
	import PublishCard from './publishcard/publishcard'
	import SubSection from "components/common/subSection"
	import {myPublishDataStatus,myPublishPageStatus} from "utils/enums/myPublish"
	import {
		getProductByPage,
		getNumberOfReleases
	} from "apis/farmer/goods.js"
	export default {
		components: {
			PublishCard,
			SubSection
		},
		data() {
			return {
				sum: 0, // 总发布数量
				//回到顶部
				scrollTop: 0,
				goodsType: myPublishDataStatus.onSale, // 当前页状态 在售 预售 审核,默认在售
				pageType: myPublishPageStatus.farmerPrimary, // 页面类型 0 农户初级农产品，1 公司初级农产品 2 公司加工农产品,默认为农户初级
				list: [{
					label: "在售",
					value: myPublishDataStatus.onSale
				}, {
					label: "预售",
					value: myPublishDataStatus.preSale
				}, {
					label: "审核",
					value: myPublishDataStatus.audit
				}],
				dataList: [],
				// 分页
				pageNo: 1,
				pageSize: 10
			}
		},
		
		methods: {
			//页面跳转
			send() {
				this.pushNewPage("sends_goods")
			},
			//  商品被删除
			deleteProduct(id){
				const index = this.dataList.findIndex(item => item.id === id)
				this.dataList.splice(index,1)
				this.sum--
			},

			/* 这里的type就是list配置里面的value */
			sectionChange(type) {
				this.goodsType = type
				this.initPage()
				this.getData()
			},
			async getData() {
				const {
					pageNo,
					pageSize,
					goodsType,
					pageType
				} = this
				const {
					code,
					data,
					msg
				} = await getProductByPage(pageNo, pageSize, goodsType)
				if (code !== 200) return this.$errorMsg(msg)
				for (const item of data['list']) {
					this.dataList.push(Object.freeze({
						...item,
						goodsType,
						pageType,
					}))
				}
				this.pageNo++

			},
			initPage() {
				this.dataList = []
				this.pageNo = 1
				this.pageSize = 10
			}

		},
		async onShow() {
			this.initPage() 
			this.getData()
			const {data:{auditNum,onSellNum,preSellNum,totalNum}} = await getNumberOfReleases(this.pageType)
			this.sum = totalNum
			this.list.forEach(item => {
				switch(item.value){
					case -1: item.label=`审核(${auditNum})`; break;
					case 1: item.label=`预售(${preSellNum})`; break;
					case 0: item.label=`在售(${onSellNum})`; break;
					default: break;
				}
			})
			// this.getSum(); //请求发布数量
			// this.getMyPublishInfo(); //请求商品数据

		},
		onReachBottom() { //到达底部
			this.getData();
		},
		onPageScroll(e) { //生命周期函数
			this.scrollTop = e.scrollTop;
		}
	}
</script>

<style>
	@import url("my_publish.css");

	.sticky {
		position: sticky;
		top: 0;
		background-color: #fff;
		z-index: 70;
	}
</style>
